package com.zuo.service.serviceImpl;

import com.zuo.entity.Department;
import com.zuo.mapper.DepartmentMapper;
import com.zuo.service.DepartmentService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author 白白旧维
 * @Date 2022/3/2 17:53
 */
@Service
@Transactional(readOnly = true)
public class DepartmentServiceImpl implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> listDepartment() {
        return departmentMapper.listDepartment();
    }

    @Override
    public List<Department> searchDepartment(String dname) {
        return departmentMapper.searchDepartment(dname);
    }

    @Override
    public Department getOne(int id) {
        return departmentMapper.getOne(id);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int updateDepartment(Department department) {
        return departmentMapper.updateDepartment(department);
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int removeDepts(String ids) {
        String[] id = ids.split(",");
        int count=0;
        for (String i : id) {
            int did=0;
            if(StringUtils.isNotBlank(i)){
                did=Integer.parseInt(i);
            }
            int result = departmentMapper.removeDept(did);
            if(result!=0){
                count++;
            }
        }
        if(count!=id.length){
            throw new RuntimeException("删除失败");
        }
        return count;
    }

    @Override
    @Transactional(rollbackFor = Throwable.class)
    public int addDept(Department department) {
        return departmentMapper.addDept(department);
    }
}
